﻿<?php

/* TravelTaste(C) 2009 - 2010
  *$ 20:35 2010-4-2
  * home.php
 * Author: Yuhuang@ncic.ac.cn
*/


 
// 初始化页面
require_once "./init.php";
require_once "./sim.php";

$action = getgpc("action");

/* 强制让用户登录 */
if(1)
if (true != check_user_login("raywill", "111111"))
{
	die("wrong user/pass");
}



if (isset($_SESSION['login']) && $_SESSION['login']== true)
{
	/* 到达这一步的时候，可以确定系统已经取得了登录用户的基本信息*/
	$uid = $_SESSION['user']['uid'];
	//echo $_SESSION['user']['user'];
}

if(1)
{
	/* 没有用户信息 _SESSION['user']没有被初始化 */
	switch($action)
	{
	case "all":	/*随便看看*/
		/*
		* t2包含了所有用户信息
		*/
		$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, "
		.TT_DBTABLEPRE."user as t2 WHERE "
		."t1.uid=t2.uid ORDER BY t1.post_date LIMIT 100");	
		break;
/*
 * 关于go和come中 t1.target = t3.target的含义：
 * state_list中提供了接待信息。对于每个注册用户，只要注册了id，填写了所在城市，
 * 那么就会自动生成一条接待信息。接待等级默认为0
 */		
	case "go":	/* 看看有没有人能接待我 */
		/* 
		 * $place: 提供我想去的地方
		 * t2: 提供别人的基本信息(所在城市)
		 */		
		$place =  determin_guest_distination();
		if ($place)
		{
			//假设能为该匿名用户明确一个地方，则查询语句如下
			$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM "
						.TT_DBTABLEPRE."user as t2 WHERE "  
						."'$place' = t2.current_city "	 
						."ORDER BY t2.service_provided LIMIT 100");
		}
		else
		{
			if (isset($_SESSION['login']) && $_SESSION['login']== true)
			{
				/* 
				 * t1: 提供我想去的地方
				 * t2: 提供别人的基本信息(所在城市)
				 */
				$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, "
							.TT_DBTABLEPRE."user as t2 WHERE "  
							."t1.uid = $uid and t1.state_type = 1 and t1.target = t2.current_city "	 
							."ORDER BY t1.post_date LIMIT 100");
			}
			else
			{
				/* 给出所有地区的接待信息*/
				$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM "
							.TT_DBTABLEPRE."user as t2 "   
							."ORDER BY t2.service_provided LIMIT 100");	
			}
		}
		break;
	case "come": /* 看看我能不能接待下别人 */
		/* 
		 * t1: 提供别人想去的地方
		 * t2: 提供别人的基本信息
		 */
		$my_city = determin_guest_location();
		if ($my_city)
		{
			$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, "		
						.TT_DBTABLEPRE."user as t2 WHERE "	
						."t1.uid = t2.uid and t1.state_type= 1 and "
						."'$my_city' = t1.target "
						."ORDER BY t1.post_date LIMIT 100");
		}
		else
		{
			if (isset($_SESSION['login']) && $_SESSION['login']== true)
			{
				/* 
				 * t1: 提供别人想去的地方
				 * t2: 提供别人的基本信息
				 */
				$my_city = $_SESSION['user']['current_city'];
				$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, "		
							.TT_DBTABLEPRE."user as t2 WHERE "	
							."t1.uid = t2.uid and t1.state_type= 1 and "
							."'$my_city' = t1.target "
							."ORDER BY t1.post_date LIMIT 100");
			}
			else
			{
				/* 显示所有希望得到接待的人的信息 */
				$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, "		
							.TT_DBTABLEPRE."user as t2 WHERE "	
							."t1.uid = t2.uid and t1.state_type= 1 "
							."ORDER BY t1.post_date LIMIT 100");			
			}
		}
	
		break;
	case "together":
		/* 
		 * $place: 提供我想去的地方
		 * t2: 提供别人想去的地方
		 * t3: 提供别人的基本信息
		 */
		$place =  determin_guest_distination(); 
		if($place)
		{
		$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t2, "		
					.TT_DBTABLEPRE."user as t3 WHERE "	
					."'$place' = t2.target and "	
					."t2.uid=t3.uid and t2.state_type = 3 " 
					."ORDER BY t2.post_date LIMIT 100");
		}
		else
		{
			if (isset($_SESSION['login']) && $_SESSION['login']== true)
			{
				/* 
				 * t1: 提供我想去的地方
				 * t2: 提供别人想去的地方
				 * t3: 提供别人的基本信息
				 */	
				$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, ".TT_DBTABLEPRE."state_list as t2, "		
							.TT_DBTABLEPRE."user as t3 WHERE "	
							."t1.uid = $uid and t3.uid != $uid and t1.target = t2.target and "	
							."t2.uid=t3.uid and t2.state_type = 3 and  t1.state_type = 3 " 	
							."ORDER BY t1.post_date LIMIT 100");				
			}
			else
			{
				/* 显示所有结伴信息 */
				$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t2, "		
							.TT_DBTABLEPRE."user as t3 WHERE "	
							."t2.uid=t3.uid and t2.state_type = 3 " 
							."ORDER BY t2.post_date LIMIT 100");				
			}
		}
		break;
	default:
		$state_list = null;
		break;
	}
	display_state_list_page($state_list, $action);
/*

		$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, "		
					.TT_DBTABLEPRE."user as t2 WHERE "	
					."t1.uid=t2.uid and t1.state_type= 2 and t1.target = $place " 	
					."ORDER BY t1.post_date LIMIT 100");
*/					

/*	
//假设能为该匿名用户明确一个地方，则查询语句如下

		$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1, "	
					.TT_DBTABLEPRE."user as t2 WHERE "	
					."t1.uid=t2.uid and t1.state_type= 1 and t1.target = $place "
					."ORDER BY t1.post_date LIMIT 100");	
*/					

}



function display_state_list_page($sl, $action)
{
	global $_SGLOBAL;
	// 显示页面
	$_SGLOBAL['view']->assign('image_root', TT_ROOT.'./images/icon');

	/*
// debug
	foreach($sl as $i)
	{
		if (!is_array($i))
			echo $i;
		else
		{
		foreach($i as $j)
			//echo $j;
			echo $j['key'];
		echo "<br>";
		}
		
	}
	*/

	
	if (isset($_SESSION['login']) && $_SESSION['login']== true)
	{
		$info = array(
			"action" => $action,
			"user_info" => $_SESSION['user'],
			"state_list" => $sl,
			"session" => $_SESSION
		);
	}
	else
	{
		$info = array(
			"action" => $action,
			"user_info" => null,
			"state_list" => $sl,
			"session" => $_SESSION
		);	
	}
	$_SGLOBAL['view']->assign("info",$info);
	$_SGLOBAL['view']->display('home_std.tpl');
}

/*
 * 获得guest用户的目标旅行位置
 */
function determin_guest_distination()
{
	/* 先尝试从SESSION中获取用户选择的位置 
	 * 获取失败，则设置为空 
	 */
	if (isset($_SESSION['distination_setting']))
	{
		$d = getgpc("distination");
		if (null != $d)	/*设置了新的地址, 则更新session */
		{
			$_SESSION['distination_setting'] = $d;
		}
		return $_SESSION['distination_setting'];
	}
	else
	{
		/* 尝试读取用户输入，看是否在本次设置位置 */
		$d = getgpc("distination"); /*设置了地址, 保存进session */
		if (null != $d)
		{
			$_SESSION['distination_setting'] = $d;
			return $_SESSION['distination_setting'];
		}
		return null;
	}
}

function determin_guest_location()
{
	if (isset($_SESSION['location_setting']))
	{
		$d = getgpc("location");
		if (null != $d)
		{
			$_SESSION['location_setting'] = $d;
		}
		return $_SESSION['location_setting'];
	}
	else
	{
		/* 尝试读取用户输入，看是否在本次设置位置 */
		$d = getgpc("location");
		if (null != $d)
		{
			$_SESSION['location_setting'] = $d;
			return $_SESSION['location_setting'];
		}
	}
	return null;
}


?>